package com.sihai.ehr.utils;

import com.sihai.core.utils.DateUtils;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * Created by IntelliJ IDEA.
 * User: Wang.Yi
 * Date: 2012-6-11
 * Time: 21:30:25
 * To change this template use File | Settings | File Templates.
 */
public class DateUtil {

    /**
     * 日期型字符串格式
     */
    public static String[] pattern = new String[]{
            "yyyy-MM", "yyyyMM", "yyyy/MM",
            "yyyyMMdd", "yyyy-MM-dd", "yyyy/MM/dd",
            "yyyyMMddHHmmss",
            "yyyy-MM-dd HH:mm:ss",
            "yyyy/MM/dd HH:mm:ss"};

    public static Date parseDate(String pDate) {
        try {
            return DateUtils.parseDate(pDate, pattern);
        } catch (Exception ex) {
            return null;
        }
    }

    /**
     * 根据给定的格式获取指定日期的字符串
     * @param date
     * @param pattern
     * @return
     */
    public static String getStrByDate(Date date,String pattern){
        DateFormat df=new SimpleDateFormat(pattern);
        return df.format(date);
    }

    /**
     * 获取两个时间的差值字符串
     *
     * @param start
     * @param end
     * @return
     */
    public static String getDiffDate(Long start, Long end) {
        if (start == null || end == null) {
            return "";
        }
        Long diff = end - start;
        DateFormat df = new SimpleDateFormat("mm分ss秒");
        return df.format(diff);
    }

    public static String getMonthsOfAge(Date beginDate, Date endDate) {
        Calendar end = Calendar.getInstance();
        end.setTime(endDate);
        Calendar begin = Calendar.getInstance();
        begin.setTime(beginDate);
        int diffmon = (end.get(1) - begin.get(1)) * 12 + (end.get(2) - begin.get(2));
        int year = diffmon / 12;
        int month = diffmon % 12;
        return year + "年" + month + "月";
    }

    /**
     * 获取指定日期和当前日期相隔的天数
     * @param date
     * @return
     */
    public static Long getDayToCurrdate(Date date) {
        if (date == null)
            return 0L;
        long mis = date.getTime();
        long cur_mis = System.currentTimeMillis();
        long diff = mis - cur_mis;
        if (diff < 0)
            return 0L;
        long day = 1000 * 60 * 60 * 24;//一天的毫秒数
        return diff / day;
    }

    public static Date addMonth(Date date, int months) {
		if (date == null) {
			throw new IllegalArgumentException("date can not be null");
		}
		Calendar calendar = new GregorianCalendar();
		calendar.setTime(date);
		calendar.add(Calendar.MONTH, months);
		return calendar.getTime();
	}

    //add.wxn
    //private static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    private static SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
    //add.wxn
    public static Date createDate(String dateStr){
        try {
            return sdf.parse(dateStr);
        } catch (ParseException e) {
            e.printStackTrace();
        }

        return createDefaultDate();
    }
    //add.wxn
    public static String formatDate(String dateStr){
        try {
            Date date = createDate(dateStr);
            return sdf.format(date);
        } catch (Exception e) {
            e.printStackTrace();
            return "01/01/1900";
        }
    }

    public static Date createDate(String dateStr, String format){
        try {
            return new SimpleDateFormat(format).parse(dateStr);
        } catch (ParseException e) {
            e.printStackTrace();
        }

        return createDefaultDate();
    }

    //add.wxn
    public static Date createDefaultDate(){
        Calendar c = Calendar.getInstance();
        c.set(Calendar.YEAR, 1900);
        c.set(Calendar.MONTH, 0);
        c.set(Calendar.DAY_OF_MONTH, 1);
        return c.getTime();
    }

    public static void main(String[] args) {
//        Calendar begin=Calendar.getInstance();
//        begin.set(1,2005);
//        begin.set(2,9);            System.out.println(begin.get(1));
//        System.out.println(begin.get(2));
//        Calendar end=Calendar.getInstance();
//        end.set(1,2008);
//        end.set(2,3);        System.out.println(end.get(2));
//        System.out.println(end.get(2)-begin.get(2));
//        String dateStr=DateUtil.getMonthsOfAge(begin.getTime(),end.getTime());
//
        //String b = new SimpleDateFormat("yyyy-MM-dd").format(createDate("01/0dd1/add"));
        //System.out.println("wxn---"+b);


        String b = new SimpleDateFormat("yyyy-MM-dd").format(createDefaultDate());
        System.out.println(b);
        String c = new SimpleDateFormat("yyyy-MM-dd").format(createDate("saeaeae"));
        System.out.println(c);
    }

}
